<?php

namespace app\common\command\task;

use app\admin\logic\ImportQuestionLogic;
use app\admin\model\Coursepaper;
use app\admin\model\Question;
use app\admin\model\Coursequestioncategory;
use app\admin\model\profession\Profession;
use app\admin\model\user\Vip;
use app\common\exception\BusinessException;
use app\common\model\User;
use PhpOffice\PhpSpreadsheet\IOFactory;
use SplFileObject;
use think\Db;
use think\Exception;

/**
 * 测试
 */
class Test
{
    /**
     * invoke
     */
    public function invoke()
    {
        // 设置每页处理的记录数
        $pageSize = 100;
        $page = 1;
        $totalCount = 0;
        $processedCount = 0;
        
        // 实例化ImportQuestionLogic类
        $importLogic = new ImportQuestionLogic();
        
        // 获取总记录数
        $totalCount = Db::name('course_paper')->count();
        echo "总共有 {$totalCount} 条记录需要处理\n";
        
        // 分页处理
        while (true) {
            // 获取当前页的数据
            $paperList = Db::name('course_paper')
                ->page($page, $pageSize)
                ->select();
            
            // 如果没有数据，说明处理完成
            if (empty($paperList)) {
                break;
            }
            
            echo "正在处理第 {$page} 页数据，共 " . count($paperList) . " 条记录\n";
            
            // 循环处理每条记录
            foreach ($paperList as $paper) {
                try {
                    // 调用公共方法更新数据
                    $importLogic->updatePaperQuestionCount($paper['course_id'], $paper['id']);
                    $processedCount++;
                    
                    // 每处理10条输出一次进度
                    if ($processedCount % 10 === 0) {
                        echo "已处理: {$processedCount}/{$totalCount}\n";
                    }
                } catch (\Exception $e) {
                    // 记录错误但继续处理
                    echo "处理试卷ID:{$paper['id']}时出错: " . $e->getMessage() . "\n";
                }
            }
            
            // 进入下一页
            $page++;
            
            // 每处理完一页暂停一下，避免服务器负载过高
            echo "第 " . ($page - 1) . " 页处理完成，已处理 {$processedCount}/{$totalCount} 条记录\n";
            usleep(100000); // 暂停0.1秒
        }
        
        echo "处理完成，共更新{$processedCount}条记录\n";
    }

    private
    function api($api, $token, $data)
    {
        $ch = curl_init();
        curl_setopt(
            $ch,
            CURLOPT_URL,
            $api
        );
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_POST, 1);
        if ($data) {
            curl_setopt(
                $ch,
                CURLOPT_POSTFIELDS,
                json_encode($data)
            );
        }

        curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $headers = array();
        $headers[] = 'Accept: application/json, text/plain, */*';
        $headers[] = 'Accept-Language: zh-CN,zh;q=0.9';
        $headers[] = 'Authorization: ' . $token;
        $headers[] = 'Connection: keep-alive';
        $headers[] = 'Content-Type: application/json';
        $headers[] = 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.64 Safari/537.36';
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        $response = curlExecByRetry($ch, 1);
        if (curl_errno($ch)) {
            throw new Exception('调用会员消费数据总数接口失败' . curl_error($ch));
        }
        curl_close($ch);
        if (!$response) {
            throw new Exception('调用会员消费数据总数接口失败');
        }
        $responseData = json_decode($response, true);
        return $responseData;
    }
}